package dao;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

import databean.ProductBean;

import utility.Util_Merchandizing;

public class GetTopSellingProductsDAO {
	
	public static List<ProductBean> getTopSellingProducts() {
		
		Document response = Util_Merchandizing.sendRequest("getTopSellingProducts.txt", null);
				
		NodeList ids=response.getElementsByTagName("productId");
		int len=ids.getLength();
		
		NodeList titles = response.getElementsByTagName("title");
		NodeList productURLs = response.getElementsByTagName("productURL");
		NodeList catalogNames = response.getElementsByTagName("catalogName");
		NodeList imageURLs = response.getElementsByTagName("imageURL");
		NodeList reviewCounts = response.getElementsByTagName("reviewCount");
		NodeList priceRangeMins = response.getElementsByTagName("priceRangeMin");
		NodeList priceRangeMaxs = response.getElementsByTagName("priceRangeMax");
		
		List<ProductBean> products = new ArrayList<ProductBean>();
		
		for(int i=0;i<len;i++){
			
			ProductBean product = new ProductBean();
			product.setProductID(geti(ids,i));
			product.setTitle(geti(titles,i));
			product.setProductURL(geti(productURLs,i));
			product.setCatalogName(geti(catalogNames,i));
			product.setImageURL(geti(imageURLs,i));
			product.setReviewCount(geti(reviewCounts,i));
			product.setPriceRangeMin(geti(priceRangeMins,i));
			product.setPriceRangeMax(geti(priceRangeMaxs,i));
			products.add(product);
			
		}
		return products;
	}
	
    private static String geti(NodeList list,int i){
    	return list.item(i).getChildNodes()
		.item(0).getNodeValue();
    }
	
}
 